/*
作用：一些画图函数
作者：孙泽宇
*/
#include"./include/SVGA.h"
#include"./include/draw.h"
#include <stdlib.h>
/*
作用：将24位颜色值转换为16位
输入：按顺序输入8位R、G、B颜色值
输出：无
返回值：16位颜色值
*/
int translateRGB(int R,int G,int B)
{
	int color;
	R = R >> 3;	//R5位
	G = G >> 2; //G6位
	B = B >> 3; //B5位
	R = R << 11;
	G = G << 5;
	color = R + G + B;//将原24位RGB转化为16位
	return color;		//返回16位RGB颜色值
}


/*
作用：获得一定范围像素信息并储存在传入空间中
输入：左上角坐标，右下角坐标，储存指针
输出：无
返回值：无
*/
void get_image(int x1,int y1,int x2,int y2,unsigned int **buffer)
{
	int i;
	int j;				//i为列，j为行 
	for(j = 0; j <= (y2 - y1); j++)
	{
		for(i = 0; i <= (x2 - x1); i++)
		{
			buffer[j][i] = getpixel_64k(x1+i,y1+j);
		}
	}
}


/*
作用：画出储存空间中的图形
输入：左上角坐标，右下角坐标,储存指针
输出：图形
返回值：无
*/
void put_image(int x1,int y1,int x2,int y2,unsigned int **buffer)
{
	int i;
	int j;				//i为行，j为列
	for(j = 0; j <= (y2 - y1); j++)
	{
		for(i = 0; i <= (x2 - x1); i++)
		{
			putpixel_64k(x1+i,y1+j,buffer[j][i]);
		}
	}
}


/*
作用：画填充矩形
输入：左上角坐标，右下角坐标，颜色
输出：填充矩形
返回值：无
*/
void fill_rectangle(int x1, int y1, int x2, int y2, unsigned int color)
{
	int i,j;
	for(j=y1;j<=y2;j++)
	{
		draw_line(x1,j,x2,j,color);
	}
}


/*
作用：画线
输入：端点坐标，颜色
输出：线
返回值：无
*/
void draw_line(int x1, int y1, int x2, int y2, unsigned int color)
{
	int	dx, dy, i;			/*直线x、y坐标差值*/
	int dx2, dy2;		/*加快运算速度的中间值*/
	int xinc, yinc;		/*判断想、y增加方向的符号值*/
	int d, dxy;			/*决策变量*/
	/*计算坐标差值和中间变量*/
	dx = abs(x2 - x1);
	dx2 = dx << 1;
	dy = abs(y2 - y1);
	dy2 = dy << 1;
	/*判断直线x坐标增加方向*/
	if (x2 > x1)
	{
		xinc = 1;
	}
	/*如果是竖直线*/
	else if (x2 == x1)
	{
		/*y坐标排序*/
		if (y1 > y2)
		{
			i = y1;
			y1 = y2;
			y2 = i;
		}

		/*画竖直线*/
		for (i = y1; i <= y2; i++)
		{
			putpixel_64k(x1, i, color);
		}

		return;
	}
	else
	{
		xinc = -1;
	}
	/*判断直线y坐标增加方向*/
	if (y2 > y1)
	{
		yinc = 1;
	}
	/*如果是水平线*/
	else if (y2 == y1)
	{
		/*x按坐标排序*/
		if(x1 > x2)
		{
			i = x1;
			x1 = x2;
			x2 = i;
		}
		/*画水平线*/
		for(i = x1; i <= x2; i++)
		{
			putpixel_64k(i, y1, color);
		}
		return;
	}
	else
	{
		yinc = -1;
	}
	/*运用Bresenham算法生成直线*/
	putpixel_64k(x1, y1, color);
	if (dx >= dy)
	{
		d = dy2 - dx;
		dxy = dy2 - dx2;
		while (dx--)
		{
			if (d <= 0)
			{
				d += dy2;
			}
			else
			{
				d += dxy;
				y1 += yinc;
			}
			x1 += xinc;
			putpixel_64k(x1, y1, color);
		}
	}
	else
	{
		d = dx2 - dy;
		dxy = dx2 - dy2;
		while (dy--)
		{
			if (d <= 0)
			{
				d += dx2;
			}
			else
			{
				d += dxy;
				x1 += xinc;
			}
			y1 += yinc;
			putpixel_64k(x1, y1, color);
		}
	}
}

/*
作用：画圆
输入：圆心坐标，半径，颜色
输出：圆圈
输出：无
*/
void draw_circle(int xc, int yc, int radius, unsigned int color)
{
	/*画圆圈的定位变量和决策变量*/
	int x, y, d;
	/*半径必须为正，否则退出*/
	if (radius <= 0)
	{
		return;
	}
	/*以下运用Bresenham算法生成圆圈。*/
	y = radius;
	d = 3 - radius << 1;
	for (x = 0; x <= y; x++)
	{
		putpixel_64k(xc + x, yc + y, color);
		putpixel_64k(xc + x, yc - y, color);
		putpixel_64k(xc - x, yc - y, color);
		putpixel_64k(xc - x, yc + y, color);
		putpixel_64k(xc + y, yc + x, color);
		putpixel_64k(xc + y, yc - x, color);
		putpixel_64k(xc - y, yc - x, color);
		putpixel_64k(xc - y, yc + x, color);
		if (d < 0)
		{
			d += x * 4 + 6;
		}
		else
		{
			d += (x - y) * 4 + 10;
			y--;
		}
	}
}


/*
作用：画填充圆形
输入：圆心坐标，半径，颜色
输出：填充圆
返回值：无
*/
void fill_circle(int xc, int yc, int radius, unsigned int color)
{
	/*画圆圈的定位变量和决策变量*/
	int x = 0,
		y = radius,
		dx = 3,
		dy = 2 - radius - radius,
		d = 1 - radius;

	/*半径必须为正，否则退出*/
	if (radius <= 0)
	{
		return;
	}
	/*以下运用Bresenham算法生成实心圆*/
	while (x <= y)
	{
		draw_line(xc - x, yc - y, xc - x + x + x, yc - y, color);
		draw_line(xc - y, yc - x, xc - y + y + y, yc - x, color);
		draw_line(xc - y, yc + x, xc - y + y + y, yc + x, color);
		draw_line(xc - x, yc + y, xc - x + x + x, yc + x, color);
		if (d < 0)
		{
			d += dx;
			dx += 2;
		}
		else
		{
			d += (dx + dy);
			dx += 2;
			dy += 2;
			y--;
		}
		x++;
	}
}


/*
作用：在数学第一第二象限画圆
输入：圆心坐标，半径，颜色
输出：上半圆
返回值：无
*/
void draw_circle_12(int xc, int yc, int radius, unsigned int color)
{
	/*画圆圈的定位变量和决策变量*/
	int x, y, d;
	/*半径必须为正，否则退出*/
	if (radius <= 0)
	{
		return;
	}
	/*以下改编Bresenham算法生成圆圈*/
	y = radius;
	d = 3 - radius << 1;
	for (x = 0; x <= y; x++)
	{
		putpixel_64k(xc + x, yc - y, color);
		putpixel_64k(xc - x, yc - y, color);
		putpixel_64k(xc + y, yc + x, color);
		putpixel_64k(xc + y, yc - x, color);
		if (d < 0)
		{
			d += x * 4 + 6;
		}
		else
		{
			d += (x - y) * 4 + 10;
			y--;
		}
	}
}


/*
作用：在数学第三象限画圆圈
输入：圆心坐标，半径，颜色
输出：左下半圆
返回值：无
*/
void draw_circle_3(int xc, int yc, int radius, unsigned int color)
{
	/*画圆圈的定位变量和决策变量*/
	int x, y, d;
	/*半径必须为正，否则退出*/
	if (radius <= 0)
	{
		return;
	}
	/*以下改编Bresenham算法生成圆圈*/
	y = radius;
	d = 3 - radius << 1;
	for (x = 0; x <= y; x++)
	{
		putpixel_64k(xc - x, yc + y, color);
		putpixel_64k(xc + y, yc - x, color);
		if (d < 0)
		{
			d += x * 4 + 6;
		}
		else
		{
			d += (x - y) * 4 + 10;
			y--;
		}
	}
}


void fill_circle_by_area(int xc, int yc, int radius, unsigned int color,unsigned int order)//将圆等分为八份，第一象限上部为0号，顺时针以此类推 
{
	/*画圆圈的定位变量和决策变量*/
	int x, y, d;
	/*半径必须为正，否则退出*/
	if (radius <= 0)
	{
		return;
	}
	/*区号必须为0~7之间的整数*/ 
	if (order < 0 ||order > 7)
	{
		return;
	}
	/*以下改编Bresenham算法生成圆圈*/
	y = radius;
	d = 3 - radius << 1;
	if(order==0) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc + x, yc - y, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==1) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc + y, yc - x, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==2) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc + y, yc + x, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==3) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc + x, yc + y, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==4) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc - x, yc + y, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==5) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc - y, yc + x, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==6) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc - y, yc - x, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
	if(order==7) 
	{
		for (x = 0; x <= y; x++)
		{
			draw_line(xc - x, yc - y, xc, yc, color);
			if (d < 0)
			{
				d += x * 4 + 6;
			}
			else
			{
				d += (x - y) * 4 + 10;
				y--;
			}
		}
	}
}
/*
作用：画新建文件夹栏的文件夹图标
输入：左上角坐标x,y
输出：图标
返回值：无
*/
void draw_topfolder(int x,int y)
{
	int i;						//循环辅助变量
	for(i = 0; i < 6; i++)
	{
		draw_line(x+i,y+i,x+7,y+i,translateRGB(243,210,113));
	}
	putpixel_64k(x,y+1,translateRGB(255,234,167));
	for(i = 1; i < 5;i++)
	{
		draw_line(x,y+i+1,x+i,y+i+1,translateRGB(255,234,167));
	}
	fill_rectangle(x+8,y,x+20,y+28,translateRGB(250,218,121));
	fill_rectangle(x,y+6,x+6,y+28,translateRGB(255,234,165));
	draw_line(x+21,y+18,x+21,y+28,translateRGB(254,225,130));
	draw_line(x+22,y+19,x+22,y+28,translateRGB(254,225,130));
	draw_line(x+7,y+6,x+7,y+24,translateRGB(255,239,187));
	draw_line(x+7,y+25,x+7,y+28,translateRGB(233,197,99));
	draw_line(x,y+29,x+6,y+29,translateRGB(255,226,140));
	for(i = 1; i < 5;i++)
	{
		draw_line(x+i,y+29+i,x+6,y+29+i,translateRGB(255,226,140));
	}
}

/*
作用：画文件显示时的文件夹图标（小）
输入：左上角坐标x,y
输出：图标
返回值：无
*/
void draw_folder_s(int x,int y)
{
	draw_line(x,y,x+3,y,translateRGB(240,207,111));
	draw_line(x+1,y+1,x+3,y+1,translateRGB(240,207,111));
	draw_line(x+2,y+2,x+3,y+2,translateRGB(240,207,111));
	putpixel_64k(x,y+1,translateRGB(255,235,172));
	draw_line(x,y+2,x+1,y+2,translateRGB(255,235,172));
	fill_rectangle(x,y+3,x+2,y+14,translateRGB(255,235,169));
	draw_line(x+3,y+3,x+3,y+12,translateRGB(255,239,187));
	draw_line(x+3,y+13,x+3,y+14,translateRGB(237,204,110));
	draw_line(x,y+15,x+2,y+15,translateRGB(255,226,140));
	draw_line(x+1,y+16,x+2,y+16,translateRGB(255,226,140));
	fill_rectangle(x+4,y,x+10,y+14,translateRGB(247,214,115));
	draw_line(x+11,y+10,x+11,y+14,translateRGB(255,224,129));
}

/*
作用：画文件显示时的文件夹图标（大）
输入：左上角坐标x,y
输出：图标
返回值：无
*/
void draw_folder_l(int x,int y)
{
	int i;							//循环辅助变量
	for(i = 0; i < 22; i++)
	{
		draw_line(x+i,y+i,x+22,y+i,translateRGB(242,208,108));
	}
	putpixel_64k(x,y+1,translateRGB(254,233,163));
	for(i = 1; i < 21; i++)
	{
		draw_line(x,y+1+i,x+i,y+1+i,translateRGB(254,233,163));
	}
	fill_rectangle(x,y+22,x+22,y+70,translateRGB(255,228,146));
	fill_rectangle(x,y+71,x+20,y+83,translateRGB(255,228,146));
	draw_line(x+21,y+71,x+21,y+83,translateRGB(234,197,98));
	draw_line(x+22,y+71,x+22,y+83,translateRGB(234,197,98));
	for(i = 0; i < 20; i++)
	{
		draw_line(x+i,y+83+i,x+20,y+83+i,translateRGB(255,228,146));
	}
	fill_rectangle(x+23,y,x+61,y+83,translateRGB(250,219,121));
	for(i = 0; i < 5; i++)
	{
		draw_line(x+67-i,y+56-i,x+62,y+56-i,translateRGB(250,219,121));
	}
	fill_rectangle(x+62,y+57,x+67,y+83,translateRGB(254,224,127));
}


/*
作用：画文件显示时的txt图标（小）
输入：左上角坐标x,y
输出：图标
返回值：无
*/
void draw_txt_s(int x,int y)
{
	int i;							//循环辅助变量
	draw_line(x,y,x+10,y,translateRGB(155,155,156));
	draw_line(x,y+1,x,y+17,translateRGB(155,155,156));
	draw_line(x+1,y+17,x+13,y+17,translateRGB(155,155,156));
	draw_line(x+13,y+3,x+13,y+16,translateRGB(155,155,156));
	fill_rectangle(x+2,y+1,x+12,y+16,translateRGB(252,253,253));
	draw_line(x+11,y,x+13,y+2,translateRGB(163,163,164));
	draw_line(x+1,y+1,x+1,y+16,translateRGB(229,229,299));
	draw_line(x+14,y+2,x+14,y+17,translateRGB(226,226,226));
	draw_line(x+10,y+1,x+10,y+3,translateRGB(166,166,166));
	draw_line(x+11,y+3,x+12,y+3,translateRGB(166,166,166));
	draw_line(x+2,y+3,x+8,y+3,translateRGB(173,173,173));
	for(i = 0; i < 5; i++)
	{
		draw_line(x+2,y+5+3*i,x+11,y+5+3*i,translateRGB(173,173,173));
	}
}	

/*
作用：画文件显示时的txt图标（大）
输入：左上角坐标x,y
输出：图标
返回值：无
*/
void draw_txt_l(int x,int y)
{
	int i;							//循环辅助变量
	draw_line(x,y,x+55,y,translateRGB(210,210,210));
	draw_line(x,y+1,x,y+103,translateRGB(209,209,209));
	draw_line(x+1,y+103,x+79,y+103,translateRGB(208,208,208));
	draw_line(x+79,y+24,x+79,y+102,translateRGB(207,207,207));
	fill_rectangle(x+1,y+1,x+78,y+102,translateRGB(247,248,249));
	draw_line(x+56,y+1,x+78,y+23,translateRGB(210,209,208));
	draw_line(x+55,y+1,x+55,y+24,translateRGB(210,210,210));
	draw_line(x+56,y+24,x+78,y+24,translateRGB(210,210,211));
	for(i = 0; i < 4; i++)
	{
		draw_line(x+7,y+9+4*i,x+54,y+9+4*i,translateRGB(206,206,206));
	}
	for(i = 0; i < 20; i++)
	{
		draw_line(x+7,y+25+4*i,x+71,y+25+4*i,translateRGB(206,206,206));
	}
}

/*
作用：画文件显示时的.c.h文件图标（小）
输入：左上角x,y
输出：图标
返回值：无
*/
void draw_ch_s(int x,int y)
{
	draw_line(x,y,x+10,y,translateRGB(155,155,156));
	draw_line(x,y+1,x,y+17,translateRGB(155,155,156));
	draw_line(x+1,y+17,x+13,y+17,translateRGB(155,155,156));
	draw_line(x+13,y+3,x+13,y+16,translateRGB(155,155,156));
	fill_rectangle(x+2,y+1,x+12,y+16,translateRGB(252,253,253));
	draw_line(x+11,y,x+13,y+2,translateRGB(163,163,164));
	draw_line(x+1,y+1,x+1,y+16,translateRGB(229,229,299));
	draw_line(x+14,y+2,x+14,y+17,translateRGB(226,226,226));
	draw_line(x+10,y+1,x+10,y+3,translateRGB(166,166,166));
	draw_line(x+11,y+3,x+12,y+3,translateRGB(166,166,166));
}

/*
作用：画文件显示时的.c.h文件图标（大）
输入：左上角x,y
输出：图标
返回值：无
*/
void draw_ch_l(int x,int y)
{
	draw_line(x,y,x+55,y,translateRGB(210,210,210));
	draw_line(x,y+1,x,y+103,translateRGB(209,209,209));
	draw_line(x+1,y+103,x+79,y+103,translateRGB(208,208,208));
	draw_line(x+79,y+24,x+79,y+102,translateRGB(207,207,207));
	fill_rectangle(x+1,y+1,x+78,y+102,translateRGB(247,248,249));
	draw_line(x+56,y+1,x+78,y+23,translateRGB(210,209,208));
	draw_line(x+55,y+1,x+55,y+24,translateRGB(210,210,210));
	draw_line(x+56,y+24,x+78,y+24,translateRGB(210,210,211));
}

/*
作用：画文件显示时的word文件图标（小）
输入：左上角x,y
输出：图标
返回值：无
*/
void draw_word_s(int x,int y)
{
	int i;						//循环辅助变量
	draw_line(x,y,x+10,y,translateRGB(155,155,156));
	draw_line(x,y+1,x,y+17,translateRGB(155,155,156));
	draw_line(x+1,y+17,x+13,y+17,translateRGB(155,155,156));
	draw_line(x+13,y+3,x+13,y+16,translateRGB(155,155,156));
	fill_rectangle(x+2,y+1,x+12,y+16,translateRGB(252,253,253));
	draw_line(x+11,y,x+13,y+2,translateRGB(163,163,164));
	draw_line(x+10,y+1,x+10,y+3,translateRGB(166,166,166));
	draw_line(x+11,y+3,x+12,y+3,translateRGB(166,166,166));
	for(i = 0; i < 3; i++)
	{
		draw_line(x+9,y+8+2*i,x+13,y+8+2*i,translateRGB(65,165,238));
	}
	fill_rectangle(x-2,y+5,x+7,y+14,translateRGB(24,90,189));
	fill_rectangle(x+2,y+7,x+3,y+12,translateRGB(255,255,255));
	draw_line(x+1,y+10,x+1,y+12,translateRGB(255,255,255));
	draw_line(x+4,y+10,x+4,y+12,translateRGB(255,255,255));
	draw_line(x,y+7,x,y+12,translateRGB(255,255,255));
	draw_line(x+5,y+7,x+5,y+12,translateRGB(255,255,255));
	putpixel_64k(x-1,y+7,translateRGB(183,204,235));
	putpixel_64k(x+6,y+7,translateRGB(183,204,235));
}

/*
作用：画文件显示时的word文件图标（大）
输入：左上角x,y
输出：图标
返回值：无
*/
void draw_word_l(int x,int y)
{
	int i;					//循环辅助变量
	draw_line(x,y,x+55,y,translateRGB(210,210,210));
	draw_line(x,y+1,x,y+103,translateRGB(209,209,209));
	draw_line(x+1,y+103,x+79,y+103,translateRGB(208,208,208));
	draw_line(x+79,y+24,x+79,y+102,translateRGB(207,207,207));
	fill_rectangle(x+1,y+1,x+78,y+102,translateRGB(247,248,249));
	draw_line(x+56,y+1,x+78,y+23,translateRGB(210,209,208));
	draw_line(x+55,y+1,x+55,y+24,translateRGB(210,210,210));
	draw_line(x+56,y+24,x+78,y+24,translateRGB(210,210,211));
	for(i = 0; i < 4; i++)
	{
		draw_line(x+43,y+40+6*i,x+71,y+40+6*i,translateRGB(43,124,211));
		draw_line(x+43,y+41+6*i,x+71,y+41+6*i,translateRGB(43,124,211));
		draw_line(x+43,y+42+6*i,x+71,y+42+6*i,translateRGB(43,124,211));
	}
	fill_rectangle(x-13,y+27,x+39,y+72,translateRGB(24,90,189));
	for(i = 0; i < 6; i++)
	{
		draw_line(x-5+i,y+39,x+2+i,y+61,translateRGB(255,255,255));
		draw_line(x+10+i,y+39,x+18+i,y+61,translateRGB(255,255,255));
	}
	for(i = 0; i < 6; i++)
	{
		draw_line(x+2+i,y+61,x+10+i,y+39,translateRGB(255,255,255));
		draw_line(x+18+i,y+61,x+25+i,y+39,translateRGB(255,255,255));
	}
}

